#include "All-merchantRegister.h"

MerchantRegisterWindow::MerchantRegisterWindow(database &db, int userId, int phoneId,QWidget *parent)
    : QDialog(parent), db(db), userId(userId),phoneId(phoneId)
{
    setWindowTitle("商户注册");
    setFixedSize(800, 650);
    
    // 设置窗口标志，使其成为对话框窗口
    setWindowFlags(Qt::Dialog | Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
    
    // 设置窗口位置居中
    if (parent) {
        move(parent->window()->frameGeometry().center() - frameGeometry().center());
    }
    
    setupUI();
}

MerchantRegisterWindow::~MerchantRegisterWindow()
{
}

void MerchantRegisterWindow::setupUI()
{
    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(15);
    mainLayout->setContentsMargins(20, 20, 20, 20);

    // 创建标题
    QLabel *titleLabel = new QLabel("商户注册申请", this);
    titleLabel->setStyleSheet("font-size: 20pt; font-weight: bold; margin-bottom: 20px;");
    titleLabel->setAlignment(Qt::AlignCenter);
    mainLayout->addWidget(titleLabel);

    // 创建表单布局
    QFormLayout *formLayout = new QFormLayout();
    formLayout->setSpacing(15);
    formLayout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
    formLayout->setLabelAlignment(Qt::AlignRight | Qt::AlignVCenter);

    // 样式
    QString labelStyle = "font-size: 14pt; font-weight: bold; color: #333;";
    QString inputStyle = "font-size: 14pt; padding: 8px; border: 1px solid #ccc; border-radius: 4px;";

    // 用户ID显示（不可编辑）
    QLabel *userIdLabel = new QLabel(QString::number(userId), this);
    userIdLabel->setStyleSheet("font-size: 14pt; padding: 8px;");
    QLabel *userIdTitleLabel = new QLabel("用户ID:", this);
    userIdTitleLabel->setStyleSheet(labelStyle);
    formLayout->addRow(userIdTitleLabel, userIdLabel);

    // 商户名称
    businessNameEdit = new QLineEdit(this);
    businessNameEdit->setStyleSheet(inputStyle);
    businessNameEdit->setPlaceholderText("请输入商户名称");
    QLabel *businessNameLabel = new QLabel("商户名称:", this);
    businessNameLabel->setStyleSheet(labelStyle);
    formLayout->addRow(businessNameLabel, businessNameEdit);

    // 商户描述
    descriptionEdit = new QLineEdit(this);
    descriptionEdit->setStyleSheet(inputStyle);
    descriptionEdit->setPlaceholderText("请输入商户描述");
    QLabel *descriptionLabel = new QLabel("商户描述:", this);
    descriptionLabel->setStyleSheet(labelStyle);
    formLayout->addRow(descriptionLabel, descriptionEdit);

    // 商户地址
    addressEdit = new QLineEdit(this);
    addressEdit->setStyleSheet(inputStyle);
    addressEdit->setPlaceholderText("请输入商户地址");
    QLabel *addressLabel = new QLabel("商户地址:", this);
    addressLabel->setStyleSheet(labelStyle);
    formLayout->addRow(addressLabel, addressEdit);


    // 联系电话
    phoneEdit = new QLineEdit(this);
    phoneEdit->setStyleSheet(inputStyle);
    phoneEdit->setPlaceholderText("请输入联系电话");
    QLabel *phoneLabel = new QLabel("联系电话:", this);
    phoneLabel->setStyleSheet(labelStyle);
    formLayout->addRow(phoneLabel, phoneEdit);

    mainLayout->addLayout(formLayout);

    // 添加说明文本
    QLabel *noteLabel = new QLabel("注意：提交申请后，管理员将会审核您的信息。\n审核通过后，您将成为平台商户。", this);
    noteLabel->setStyleSheet("font-size: 12pt; color: #666; margin-top: 10px;");
    noteLabel->setAlignment(Qt::AlignCenter);
    mainLayout->addWidget(noteLabel);

    // 添加按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->setSpacing(15);

    registerButton = new QPushButton("提交申请", this);
    registerButton->setStyleSheet("font-size: 14pt; padding: 10px 20px; border-radius: 4px; background-color: #4CAF50; color: white;");
    connect(registerButton, &QPushButton::clicked, this, &MerchantRegisterWindow::onRegisterClicked);

    cancelButton = new QPushButton("取消", this);
    cancelButton->setStyleSheet("font-size: 14pt; padding: 10px 20px; border-radius: 4px;");
    connect(cancelButton, &QPushButton::clicked, this, &MerchantRegisterWindow::onCancelClicked);

    buttonLayout->addStretch();
    buttonLayout->addWidget(registerButton);
    buttonLayout->addWidget(cancelButton);
    buttonLayout->addStretch();

    mainLayout->addLayout(buttonLayout);
    QString sql =QString("select review_status,name_object,phone_id,address_id,bus_comment,phone from review where user_id = %1").arg(userId);
    QSqlQuery query = db.exec(sql,"搜索申请状态");
    query.next();
    if(query.value(0) == 0){
        businessNameEdit->setText(query.value(1).toString());
        businessNameEdit->setReadOnly(true);
        phoneEdit->setText(query.value(5).toString()); // 使用 phone 字段
        phoneEdit->setReadOnly(true);
        addressEdit->setText(query.value(3).toString());
        addressEdit->setReadOnly(true);
        descriptionEdit->setText(query.value(4).toString());
        descriptionEdit->setReadOnly(true);
        registerButton->setStyleSheet("font-size: 14pt; padding: 10px 20px; border-radius: 4px; background-color: #d4d6d5; color: white;");
        registerButton->setEnabled(false);
    }else{
        //填充初始电话
        sql =QString("select phone from phone where phone_id = %1").arg(phoneId);
        query = db.exec(sql);
        query.next();
        defaultPhone =  query.value(0).toString();
        phoneEdit->setText(defaultPhone);
    }
}

void MerchantRegisterWindow::onRegisterClicked()
{
    // TODO: 实现商户注册逻辑
    QString bussinessName = businessNameEdit->text();
    QString description = descriptionEdit->text();
    QString address = addressEdit->text();
    QString phone = phoneEdit->text();
    qDebug()<<userId<<" "<<bussinessName <<" "<<description<<" "<<address <<" "<<phone;
    if(defaultPhone == phone){
        QMessageBox::information(this, "提示", "手机号未变更");
    }
    QString sql = QString("insert into review(user_id,review_status,identity_object,name_object,phone_id,address_id,bus_comment,phone)"
                          "values(%1,0,2,'%2','%3','%4','%5','%6')").arg(userId).arg(bussinessName).arg(phoneId).arg(address).arg(description).arg(phone);
    qDebug()<<sql;
    db.exec(sql,"151-注册商户语句");
    QMessageBox::information(this, "提示", "商户注册申请已发送至管理员后台\n请耐心等待");
    accept();
}

void MerchantRegisterWindow::onCancelClicked()
{
    reject();
} 
